package com.qf.dao.Impl;/*
    Dell
    2021/10/2
    11:03
*/

import com.qf.dao.HealthDao;
import com.qf.entiy.Health;
import com.qf.entiy.Organ;
import com.qf.util.DruidUtil;
import com.qf.util.Page;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HealthDaoImpl implements HealthDao {
    private QueryRunner queryRunner  = new QueryRunner();
    @Override
    public List<Health> showAllHealth(Page page) {
        try {
            String sql = "select id, projectname, status, money, deadline, h.organId, organname from health as h inner join organ o on h.organId = o.organId limit ?,?;";
            List<Map<String, Object>> query = queryRunner.query(DruidUtil.getConnection(), sql, new MapListHandler(),page.getStartRow(),page.getPageSize());
            if(query==null){
                return null;
            }
            List<Health> healthList = new ArrayList<>();
            for (Map<String, Object> map : query) {
                Health health  = new Health();
                Organ organ  = new Organ();
                BeanUtils.populate(health, map);
                BeanUtils.populate(organ, map);
                health.setOrgan(organ);
                healthList.add(health);
            }
            return healthList;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage()+":连接查询查所有出现了异常");
        }
    }

    @Override
    public Long countHealth() {
        try {
            return queryRunner.query(DruidUtil.getConnection(),"select count(*) from Health;", new ScalarHandler<>());
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+":统计所有条数出现了异常");
        }
    }

    @Override
    public Integer insertHealth(Health health, Integer organId) {
        try {
            Object[] params = {health.getProjectname(),health.getStatus(),health.getMoney(),health.getDeadline(),organId};
            String sql = "insert into Health(projectname,status,money,deadline,organId) values(?,?,?,?,?);";
            return queryRunner.update(DruidUtil.getConnection(),sql,params);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+":新增对象出现了异常");
        }
    }

    @Override
    public Integer updateHealth(Health health, Integer organId) {
        try {
            Object[] params = {health.getProjectname(),health.getStatus(),health.getMoney(),health.getDeadline(),organId,health.getId()};
            String sql = "update Health set projectname = ?,status = ?,money = ?,deadline = ?,organId = ? where id = ?;";
            return queryRunner.update(DruidUtil.getConnection(),sql,params);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+":修改对象出现了异常");
        }
    }

    @Override
    public Integer deleteHealth(Integer organId) {
        try {
            return queryRunner.update(DruidUtil.getConnection(),"delete from Health where id = ?",organId);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+":删除对象出现了异常");
        }
    }
}
